<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>主单领取</title>
    <% include("/common/common_css_js.html"){} %>
</head>
<body>
    <div style="padding-bottom: 5px;">
        <div id="createMasterBillCodeForm"  borderStyle="border-top:0;">
                <span>领单部门：</span>
                <input id="keyGenerateDepartmentId" name="keyGenerateDepartmentId" class="mini-treeselect" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                       required="true" allowInput="true" showNullItem="true" nullItemText="请选择..."/>
                <span class="separator"></span>
                <span>航空公司：</span>
                <input id="keyXtAirlinesId" name="keyXtAirlinesId" class="mini-combobox" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                       required="true" allowInput="true" showNullItem="true" nullItemText="请选择..."/>
                <span class="separator"></span>
                <span>领单日期：</span>
                <input id="keyGenerateDate" name="keyGenerateDate" class="mini-datepicker" value="${currentDate}"  format="yyyy-MM-dd"/>
                <span class="separator"></span>
                <span>领单人：</span>
                <input id="keyGenerateUserId" name="keyGenerateUserId" class="mini-hidden" value="${userId}"/>
                <input id="keyGenerateUserName" name="keyGenerateUserName" class="mini-textbox" value="${userName}" enabled="false"/>

                <div style="margin-top: 5px"></div>

                <span>单号范围：</span>
                <input id="startCode" name="startCode"  class="mini-textbox" required="true" requiredErrorText="起始主单号不能为空"/>--<input id="endCode" name="endCode"  class="mini-textbox" required="true" requiredErrorText="结束主单号不能为空"/>
                <span class="separator"></span>
                <span>代理人：</span>
                <input id="keyAgentCompanyId" name="keyAgentCompanyId" class="mini-combobox" style="width:400px;" textField="name" valueField="id" emptyText="请选择..."
                       required="true" allowInput="false" showNullItem="true" nullItemText="请选择..."/>

                <span class="separator"></span>

                <% if(so.hasPermission("bMasterBillCode:generate")){%>
                    <a class="mini-button" iconCls="icon-add" onclick="generateCode()">生成</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("bMasterBillCode:save")){%>
                    <a class="mini-button" iconCls="icon-save" onclick="saveCode()">保存</a>
                <% } %>

        </div>
    </div>

    <div class = "mini-fit">
        <div id="datagrid1" class="mini-datagrid"
             style="width: 100%; height: 100%;"
             url="${base}/pages/bill_code_manage/b_master/showCode?userId=${userId}" idField="id"
             showPager="false" pageSize="1000" sizeList="[1000,2000]"
             multiSelect="true"
             allowResize="true"
             allowCellWrap="true">
            <div property="columns">
                <div type="checkcolumn" ></div>
                <div field="id" width="140" headerAlign="center" align="center" allowSort="true">主单号</div>
                <div field="airlinesId" width="140" headerAlign="center" align="left" allowSort="true" data-options='{cacheName:"airlines"}' renderer="commonRender">航空公司</div>
                <div field="useStatusId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">使用状态</div>
                <div field="generateDepartmentId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"department"}' renderer="commonRender">领单部门</div>
                <div field="generateDate" width="140" headerAlign="center" align="center" allowSort="true" dateFormat="yyyy-MM-dd" renderer="ondayRenderer">领单日期</div>
                <div field="generateUserId" width="140" headerAlign="center" align="center" data-options='{cacheName:"sysUser"}' renderer="commonRender">领单人</div>
                <div field="authUseDepartmentId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"department"}' renderer="commonRender">授权使用部门</div>
                <div field="useDepartmentId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"department"}' renderer="commonRender">授权使用部门</div>
                <div field="useUserId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysUser"}' renderer="commonRender">使用人</div>
                <div field="agentCompanyId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysCompany"}' renderer="commonRender">代理人</div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        mini.parse();
        let createMasterBillCodeForm = new mini.Form("#createMasterBillCodeForm");
        let keyGenerateDepartmentId = mini.get("keyGenerateDepartmentId");
        let keyXtAirlinesId = mini.get("keyXtAirlinesId");
        let keyGenerateDate = mini.get("keyGenerateDate");
        let keyGenerateUserId = mini.get("keyGenerateUserId");
        let keyGenerateUserName = mini.get("keyGenerateUserName");
        let startCode = mini.get("startCode");
        let endCode = mini.get("endCode");
        let keyAgentCompanyId = mini.get("keyAgentCompanyId");
        let datagrid1 = mini.get("datagrid1");


        $(function () {
            // 部门下拉列表初始化
            let xtDepartmentJsonData=commonRenderDropList("department");
            keyGenerateDepartmentId.loadList(xtDepartmentJsonData,"id","pid");

            // 航空公司初始化
            let xtAirlinesJsonData=commonRenderDropList("airlines");
            keyXtAirlinesId.load(xtAirlinesJsonData);

            // 代理公司（代理人）初始化
            let agentCompanyJsonData=commonRenderDropList("sysCompany");
            keyAgentCompanyId.load(agentCompanyJsonData);


        });

        /**
         * 生成单号
         */
        function generateCode() {
            // 必须先验证，否则后面的判断会一直为true
            createMasterBillCodeForm.validate();
            if (createMasterBillCodeForm.isValid() == false){
                public_showErrorTips("请将生成主单号信息填写完整！");
                return;
            }

            let startCodeValue=startCode.getValue();
            let endCodeValue=endCode.getValue();


            // 起始单号与结束单号一定要求是全部是数字字符
            if(!public_isNumber(startCodeValue) || !public_isNumber(endCodeValue)){
                public_showErrorTips("起始单号或结束单号中包含非数字字符!");
                return ;
            }
            // 起始单号与结束单号长度一定要求长度达到8位，不能多也不能少
            if(startCodeValue.length!=8 || endCodeValue.length!=8){
                public_showErrorTips("起始单号或结束单号长度不等于8!");
                return ;
            }
            // 起始单号要小于结单号
            if(startCodeValue>endCodeValue){
                public_showErrorTips("起始主单号大于结束主单号，不能生成!");
                return;
            }
            // 取起始单号的最后一位
            let codeSuffix = parseInt(startCodeValue.substring(7));
            if(codeSuffix>6){
                public_showErrorTips("起始单号的最后一位必须小于或等于6!");
                return;
            }

            let data = createMasterBillCodeForm.getData(true);

            let ret = public_getServerData("${base}/pages/bill_code_manage/b_master/generateCode",data);
            if(ret.ok) {
                public_showTip(ret);
                // 重新刷新数据
                datagrid1.load();
            }

        }

        /**
         * 保存入库
         */
        function saveCode() {
            let rows=datagrid1.getSelecteds();
            if (rows.length > 0){
                let ret = public_getServerData("${base}/pages/bill_code_manage/b_master/saveCode",rows);
                public_showTip(ret);
            }
            else{
                public_showErrorTips("请选择要保存的单号！");
            }
        }

    </script>
</body>
</html>